package com.hqyj.controller;

import com.hqyj.dao.AchievementDao;
import com.hqyj.dao.UserDao;
import com.hqyj.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/jpauser")
public class JPAUserController {

    @Autowired
    UserDao userDao;
    @Autowired
    AchievementDao achievementDao;

   /* 查询销售人员*/
/*    @GetMapping
    @RequestMapping("/alluser")
    String allUser(Model m) {
        List<User> users = userDao.QueryAll();
        m.addAttribute("users",users);
        return "admin/userlist";
    }*/
 /*   分页查询*/
 @GetMapping
 @RequestMapping(value = "/pageUser")
 String pageUser(Model m,Integer pageNum){
     System.out.println("12122121"+pageNum);
     if (pageNum == null){
         pageNum = 1;
     }
     Pageable pageable = PageRequest.of(pageNum-1,2);
     Page<User> page= userDao.QueryAll(pageable);
     int num=achievementDao.getinfonum();
     m.addAttribute("page",page);
     m.addAttribute("num",num);
     System.out.println(num+"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
     System.out.println("666262222"+page);
     return "admin/userlist";
 }

/*删除销售人员*/
   @RequestMapping("/deleteUser/{id}")
   @ResponseBody
     String deleteByUser(@PathVariable int id) {
       /*  String num = userDao.deleteByUser(id);*/
       userDao.deleteById(id);
       return "success";
   }

   /*添加销售人员*/
   @GetMapping(value="/addUser")
       String addUser(){
        return "admin/adduser";
       }
       @PostMapping("/addUser")
       Object addByUser(int id,String account,String password,String name,String sex,String phone ) {
User users = new User();
users.setId(id);
users.setAccount(account);
users.setPassword(password);
users.setName(name);
users.setRole_id("2");
users.setSex(sex);
users.setPhone(phone);
userDao.save(users);
return "redirect:pageUser";
    }
    /*修改销售人员*/
@RequestMapping(value="/updateUser/{id}",method = RequestMethod.GET)
String updateUser(@PathVariable String id,Model m){
    System.out.println("66666666"+id);
   User users = userDao.getOne(Integer.parseInt(id));
    System.out.println("2222222"+users);
    m.addAttribute("users",users);
    return "admin/updateuser";
}
@GetMapping
    @RequestMapping(value = "/updateUser/{id}",method = RequestMethod.PUT)
   Object updateByuser(@PathVariable Integer id,User user) {
       user.setId(id);
        System.out.println("7777777"+id);
        userDao.updateById(user);
        return "redirect:../pageUser";
    }

    @GetMapping
    @RequestMapping(value = "return",method = RequestMethod.PUT)
    String return1() {
    return "redirect:../pageUser";
    }

}
